package com.qf.java2106.test;

import com.qf.java2106.mapper.IAccountMapper;
import com.qf.java2106.mapper.IPersonMapper;
import com.qf.java2106.pojo.Account;
import com.qf.java2106.pojo.Person;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author q
 * @version V1.0
 * @date 2021/11/04
 **/
public class MybatisTest1104 {

    SqlSession sqlSession = null;
    IPersonMapper perMapper = null;
    IAccountMapper accountMapper = null;

    /**
     * set
     * @throws Exception
     */
    @Test
    public void testUpdatePersonByIdUseSet() throws Exception {
        Person per = new Person();
        per.setPerId(2);
        per.setPerAddress("南京");
        int i = perMapper.updatePersonByIdUseSet(per);
        System.out.println("改变条数  -->" + i);
    }

    /**
     * Trim
     * @throws Exception
     */
    @Test
    public void testFindPersonByConditionTrim() throws Exception {
        Person per = new Person();
        per.setPerName("%索%");
        per.setPerGender(0);
        List<Person> personList = perMapper.findPersonByCondition(per);
        personList.forEach(System.out::println);
    }

    /**
     * choose when otherwise
     * @throws Exception
     */
    @Test
    public void testFindPersonByConditionChoose() throws Exception {
        Person per = new Person();
        //per.setPerName("%索%");
        //per.setPerGender(0);
        //per.setPerAddress("杭州");
        List<Person> personList = perMapper.findPersonByConditionChoose(per);
        personList.forEach(System.out::println);
    }

    /**
     * 动态SQL where if
     * @throws Exception
     */
    @Test
    public void testFindPersonByCondition() throws Exception {
        Person per = new Person();
        per.setPerName("%索%");
        per.setPerGender(0);
        List<Person> personList = perMapper.findPersonByCondition(per);
        personList.forEach(System.out::println);
    }

    /**
     * 测试懒加载
     * @throws Exception
     */
    @Test
    public void testFindPersonById() throws Exception {
        Person person = perMapper.findPersonById(1);
        //System.out.println(person);
    }

    /**
     * 批量插入
     * @throws Exception
     */
    @Test
    public void testBatchInsertAccount() throws Exception {
        List<Account> accounts = new ArrayList<>();
        Account account1 = new Account();
        Account account2 = new Account();
        Account account3 = new Account();
        account1.setAccNum("2001");
        account2.setAccNum("2002");
        account3.setAccNum("2003");
        account1.setPerName("提莫");
        account2.setPerName("提莫");
        account3.setPerName("提莫");
        account1.setAccBalance(700d);
        account2.setAccBalance(800d);
        account3.setAccBalance(900d);
        accounts.add(account1);
        accounts.add(account2);
        accounts.add(account3);
        int i = accountMapper.batchInsertAccount(accounts);
        System.out.println("改变行数 --> " + i);
    }

    @Before
    public void init() throws Exception {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        sqlSession = factory.openSession();
        perMapper = sqlSession.getMapper(IPersonMapper.class);
        accountMapper = sqlSession.getMapper(IAccountMapper.class);
    }

    @After
    public void destroy() throws Exception {
        sqlSession.commit();
        sqlSession.close();
    }

}
